using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Waaahsabi.Controls.DWM
{
	public class TraceViewItem : Control 
	{
		#region ConnectionId DependencyProperty
		public static readonly DependencyProperty ConnectionIdProperty =
		DependencyProperty.Register
			(
			"ConnectionId",
			typeof(Guid),
			typeof(TraceViewItem),
			new PropertyMetadata(Guid.Empty)
			);

		public Guid ConnectionId
		{
			get { return (Guid)GetValue(ConnectionIdProperty); }
			set { SetValue(ConnectionIdProperty, value); }
		}
		#endregion

		#region CurrentValue DependencyProperty
		public static readonly DependencyProperty CurrentValueProperty =
		DependencyProperty.Register
			(
			"CurrentValue",
			typeof(double),
			typeof(TraceViewItem),
			new PropertyMetadata(null)
			);

		public double CurrentValue
		{
			get { return (double)GetValue(CurrentValueProperty); }
			set { SetValue(CurrentValueProperty, value); }
		}
		#endregion

		#region StatusText DependencyProperty
		public static readonly DependencyProperty StatusTextProperty =
		DependencyProperty.Register
			(
			"StatusText",
			typeof(string),
			typeof(TraceViewItem),
			new PropertyMetadata("--")
			);

		public string StatusText
		{
			get { return (string)GetValue(StatusTextProperty); }
			set { SetValue(StatusTextProperty, value); }
		}
		#endregion

		#region InitialTime DependencyProperty
		public static readonly DependencyProperty InitialTimeProperty =
		DependencyProperty.Register
			(
			"InitialTime",
			typeof(double),
			typeof(TraceViewItem),
			new PropertyMetadata(null)
			);

		public double InitialTime
		{
			get { return (double)GetValue(InitialTimeProperty); }
			set { SetValue(InitialTimeProperty, value); }
		}
		#endregion

		private Storyboard _progressStoryboard;
		private DoubleAnimation _progressAnimation;
		private ProgressBar _progress;

		public TraceViewItem()
		{
			DefaultStyleKey = typeof (TraceViewItem);
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_progress = GetTemplateChild("progress") as ProgressBar;
			if(_progress != null)
			{
				_progressStoryboard = new Storyboard();
				_progressAnimation = new DoubleAnimation();
				_progressAnimation.From = InitialTime;
				_progressAnimation.To = 0;
				_progressAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(InitialTime));
				_progressStoryboard.Children.Add(_progressAnimation);
				
				Storyboard.SetTarget(_progressAnimation, _progress);
				Storyboard.SetTargetProperty(_progressAnimation, new PropertyPath(ProgressBar.ValueProperty));
				_progressStoryboard.Begin();

				_progress.ValueChanged += ProgressValueChanged;
			}
		}

		void ProgressValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			try
			{
				StatusText = string.Format("{0:0.00} s", e.NewValue / 1000);
			}
			catch
			{
				StatusText = "InvldFmt";
			}
		}

		public void UpdateStatus(double remainingTime)
		{
			if (_progressStoryboard == null || _progressAnimation == null)
				return;
			_progressStoryboard.Stop();
			_progressAnimation.From = remainingTime;
			_progressAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(remainingTime));
			_progressStoryboard.Begin();
		}
	}
}